package com.ftf.tp.blog.interfaces.controller;

import com.ftf.tp.blog.application.dto.ArticlePageDTO;
import com.ftf.tp.blog.application.dto.command.PublishArticleCommand;
import com.ftf.tp.blog.application.service.command.ArticleCommandService;
import com.ftf.tp.blog.application.service.representation.ArticleRepresentation;
import com.ftf.tp.blog.application.vo.ArticleIndexVO;
import com.ftf.tp.blog.application.vo.ArticleInfoVO;
import com.ftf.tp.common.core.annotation.ResponseResult;
import com.ftf.tp.common.core.utils.CommonUtil;
import com.ftf.tp.common.cqrs.handler.Dispatcher;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文章controller
 *
 * @author tanghao
 * @date 2021/10/18
 */
@Slf4j
@RestController
@ResponseResult
@RequestMapping("/blog/article")
@Api(tags = "文章")
public class ArticleController {

    @Resource(name = "standardDispatcher")
    private Dispatcher standardDispatcher;

    @Resource
    private ArticleCommandService articleCommandService;

    @Resource
    private ArticleRepresentation articleRepresentation;

    /**
     * 发布文章
     */
    @ApiOperation("发布文章")
    @PostMapping("/auth/publishArticle")
    public void publishArticle(@RequestBody @Validated PublishArticleCommand publishArticleCommand, BindingResult bindingResult) {
        CommonUtil.paramValid(bindingResult);
        standardDispatcher.dispatch(publishArticleCommand);
    }

    /**
     * 分页查询文章列表
     *
     * @param articlePageDTO 分页查询条件
     */
    @PostMapping("/pageBlog")
    @ApiOperation("首页分页 - 目前只有文章")
    public List<ArticleIndexVO> pageBlog(@RequestBody ArticlePageDTO articlePageDTO) {
        return articleRepresentation.pageBlog(articlePageDTO);
    }

    /**
     * 根据文章id获取文章详情
     *
     * @param articleId 文章id
     * @return 文章详情
     */
    @ApiOperation("根据文章id获取文章详情")
    @GetMapping("/getArticleInfoById")
    public ArticleInfoVO getArticleInfoById(@RequestParam("articleId") Long articleId) {
        return articleRepresentation.getArticleInfoById(articleId);
    }


    /**
     * 浏览blog
     *
     * @param userId    用户id 游客为空
     * @param articleId 文章id
     */
    @GetMapping("/browseBlog")
    @ApiOperation(value = "浏览blog", notes = "当在blog页面停留1分钟以上或者blog从头划到尾 调此接口 游客的话 userId传null即可")
    public void browseBlog(@RequestParam(value = "userId", required = false) Long userId,
                           @RequestParam("articleId") Long articleId, HttpServletRequest httpServletRequest) {
        articleCommandService.browseArticle(userId, articleId, httpServletRequest);
    }


    @ApiOperation("爬博客-csdn")
    @PostMapping("/climbBlog")
    public void climbBlog(@RequestParam("userName") String userName, @RequestParam("userId") Long userId
            , @RequestParam("authorName") String authorName, @RequestParam("bigTag") Long bigTag) {
        articleCommandService.climbBlog(userName, userId, authorName, bigTag);
    }


    /**
     * 创建索引
     */
    @GetMapping("createIndex")
    public void createIndex() throws Exception {
        articleCommandService.createIndex();
    }

    /**
     * 查询
     */
    @GetMapping("/get")
    public void get() throws Exception {
        articleCommandService.get();
    }

}
